package org.example.design.代理.动态;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
/**
 * @Auther:Liu
 * @Testname:CGLibProxyHandler
 * @Date:2025/8/15 16:21
 */
public class CGLibProxyHandler implements MethodInterceptor {
    private Object target;

    public Object CGLibProxyHandler(Object target) {
        this.target = target;
        Enhancer enhancer = new Enhancer();
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        System.out.println("开始了动态代理");
        System.out.println(method.getName());
        System.out.println("结束了动态代理");
        return method.invoke(target, args);
    }
}
